package com.eascs.o2o.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.o2o.common.exception.NoAccessException;
import com.eascs.o2o.common.vo.ModelResult;
import com.eascs.o2o.uc.controller.UserRestController;
import com.eascs.o2o.uc.entity.UserEntity;
import com.eascs.site.view.ViewContext;
import com.eascs.web.context.RequestContext;

public class AccessInterceptorAdapter extends HandlerInterceptorAdapter {
	private final Logger logger = LoggerFactory.getLogger(AccessInterceptorAdapter.class);

	private final static String ACCESS_UC_CENTER_URL = "/account/";
	/**private final static String ACCESS_UC_query_URL = "/loan/"; */
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		boolean isCheckAccess = false;//false无需验证
		String requestURL = RequestContext.getRequestURI();
		if (requestURL.indexOf(ACCESS_UC_CENTER_URL) != -1 ) {
			isCheckAccess = true;
		}
		/**if (requestURL.indexOf(ACCESS_UC_query_URL) != -1 ) {
			isCheckAccess = true;
		}*/
		if (isCheckAccess) {
			ModelResult<UserEntity> modelResult = UserRestController.isLogin(request);
			if (!modelResult.isSuccess() || modelResult.getModel() == null) {
				throw new NoAccessException("No Access request :" + RequestContext.getRequestURI());
			}
		}
		ModelResult<UserEntity> memberResult=UserRestController.isLogin(request);
		UserEntity userEntity=memberResult.getModel();
		if (userEntity != null) {
			ViewContext.put("userEntity", userEntity);
		}
		return super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}
}
